﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace ttt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 生成页
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destiny"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string source = this.textBox1.Text.Trim();
            if (source == "") { return; }

            string destiny = this.textBox2.Text;

            if (destiny == "")
            {
                destiny = source.Substring(source.LastIndexOf("/") + 1, source.Length - source.LastIndexOf("/") - 1);

                destiny = destiny.Substring(0, destiny.LastIndexOf(".") + 1) + "html";
            }

            create(source, destiny);

            FileInfo fi = new FileInfo(destiny);

            System.Diagnostics.Process.Start(fi.DirectoryName);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.ShowDialog();
            this.textBox2.Text = openFileDialog1.FileName;
        }

        /// <summary>
        /// 生成页
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destiny"></param>
        private void create(string source, string destiny)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(source);

            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

            Stream receiveStream = myHttpWebResponse.GetResponseStream();

            Encoding encode = System.Text.Encoding.UTF8;
            if (this.comboBox1.SelectedItem != null && this.comboBox1.SelectedItem.ToString().Equals("Default"))
            { encode = System.Text.Encoding.Default; }
            //Encoding encode1 = System.Text.Encoding.gbk;
            StreamWriter sw = new StreamWriter(destiny, false, encode);

            StreamReader readStream = new StreamReader(receiveStream, encode);

            Char[] read = new Char[256];

            int count = readStream.Read(read, 0, 256);

            string str = "";

            while (count > 0)
            {
                str = new String(read, 0, count);
                sw.Write(str);

                count = readStream.Read(read, 0, 256);
            }

            sw.Close();
            readStream.Close();
        }

        /// <summary>
        /// 查看源代码
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        private string seeSource(string source)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(source);

            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

            Stream receiveStream = myHttpWebResponse.GetResponseStream();

            Encoding encode = System.Text.Encoding.UTF8;
            if (this.comboBox1.SelectedItem != null && this.comboBox1.SelectedItem.ToString().Equals("Default")) { encode = System.Text.Encoding.Default; }
            //Encoding encode1 = System.Text.Encoding.gbk;

            StreamReader readStream = new StreamReader(receiveStream, encode);

            Char[] read = new Char[256];

            int count = readStream.Read(read, 0, 256);

            string str = "";

            while (count > 0)
            {
                str += new String(read, 0, count);

                count = readStream.Read(read, 0, 256);
            }

            readStream.Close();
            return str;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.Items.Insert(0, "UTF8");
        }

        /// <summary>
        /// 查看源文件
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string source = this.textBox1.Text.Trim();
            if (source == "") { return; }

            string temp = seeSource(source);
            this.richTextBox1.Text = temp;
        }
    }
}